package com.aacar.service.impl;

import com.aacar.dao.OrderDao;
import com.aacar.dao.OrderDemandDao;
import com.aacar.dao.OrderPayDao;
import com.aacar.dao.WasherOrderDao;
import com.aacar.dto.ReceOrderDto;
import com.aacar.dto.WasherOrderListDto;
import com.aacar.entity.Order;
import com.aacar.entity.OrderDemand;
import com.aacar.entity.OrderPay;
import com.aacar.service.WasherOrderService;
import com.aacar.utils.*;
import com.aacar.vo.WasherOrderDetailVo;
import com.aacar.vo.WasherOrderListVo;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Repository("washerOrderService")
public class WasherOrderServiceImpl implements WasherOrderService {

	private Log logger = LogFactory.getLog(WasherOrderServiceImpl.class);



	@Resource
	private WasherOrderDao WasherOrderDao;

	@Resource
	private OrderDao orderDao;

	@Resource
	private OrderPayDao orderPayDao;

	@Resource
	private OrderDemandDao orderDemandDao;




	@Override
	public Pagination<WasherOrderListVo> getWasherOrderListPage(WasherOrderListDto WasherOrderListDto) {
		Pagination<WasherOrderListVo> pagination = new Pagination<WasherOrderListVo>();
		Page page = WasherOrderDao.getWasherOrderListCount(WasherOrderListDto);
		page.setPageNumber(WasherOrderListDto.getPageNumber());
		page.setPageSize(WasherOrderListDto.getPageSize());
		WasherOrderListDto.setStartRs(page.getStartRs());
		List<WasherOrderListVo> resultlist = WasherOrderDao.getWasherOrderListPage(WasherOrderListDto);
		for(WasherOrderListVo washerOrderListVo: resultlist){
			//处理接单倒计时
			washerOrderListVo.setReceOrderCountDown(
					processReceOrderCountDown(washerOrderListVo.getSendOrderTime()));
			//处理距离
			washerOrderListVo.setDistance("2.1km");
		}
		pagination.setPage(page);
		pagination.setResultList(resultlist);
		return pagination;
	}

	private String processReceOrderCountDown(String sendOrderTime){
		return DateUtil.caclCountDownTime(DateUtil.str2Date(DateUtil.defaultPattern2,sendOrderTime),30);
	}

	@Override
	public WasherOrderDetailVo getWasherOrderDetail(String orderNo) {
		//查询当前订单状态
		Order order = orderDao.getOrder(orderNo);
		if(order!=null){
			if(!"R03".equals(order.getOrderStatus())){
				String payOrderNo = "PA"+orderNo+"-1";
				WasherOrderDetailVo washerOrderDetailVo = WasherOrderDao.getWasherOrderDetail(payOrderNo);
				//处理接单倒计时
				washerOrderDetailVo.setReceOrderCountDown(processReceOrderCountDown(washerOrderDetailVo.getSendOrderTime()));
				//处理距离
				washerOrderDetailVo.setDistance("2.1km");
				return washerOrderDetailVo;
			}
		}
		return null;
	}

	@Override
	public String receOrder(ReceOrderDto receOrderDto) {
		String orderNo = receOrderDto.getOrderNo();
		Date now = DateUtil.getNowDate();

		//查询订单状态 如果不是等待接单 返回该订单已经接单 更新改订单的派单表 已经接单
//		Order order = orderDao.getOrder(orderNo);
//		if("R02".equals(order.getOrderStatus())){
//
//		}


		if("1".equals(receOrderDto.getUpdateBookTimeFlag())){
			//更新需求订单
			OrderDemand orderDemand = new OrderDemand();
			orderDemand.setBookStartTime(receOrderDto.getBookStartTime());
			orderDemand.setBookEndTime(receOrderDto.getBookEndTime());
			orderDemand.setOrderNo(orderNo);
			orderDemand.setModifyTime(now);
			orderDemandDao.update(orderDemand);
		}

		if("1".equals(receOrderDto.getRaisePriceFlag())){
			//加价操作 生成加价支付订单-2 修改主订单状态
			OrderPay orderPay = new OrderPay();
			orderPay.setOrderNo(orderNo);
			String payOrderNo = "PA"+orderNo+"-2";
			orderPay.setPayOrderNo(payOrderNo);
			orderPay.setPayOrderType("2");
			orderPay.setPayStatus("PA01"); //支付状态(PA01-待支付，PA02-已支付 ,PA03-已取消)
			orderPay.setPaySubStatus("PA01");
			orderPay.setOrderAmount(receOrderDto.getRaisePrice());
			orderPay.setPayAmount(receOrderDto.getRaisePrice());
			orderPayDao.save(orderPay);

			//更新主订单
			Order order = new Order();
			order.setOrderStatus("R03");
			order.setOrderStatusName("补差价");
			order.setOrderSubStatus("R03");
			order.setOrderNo(orderNo);
			order.setModifyTime(DateUtil.getNowDate());
			orderDao.update(order);

		}else{
			//不加价操作 生成业务订单 修改 主订单状态 需求订单状态


		}
		return null;
	}
}